package com.example.wechatlistview.fragments;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.wechatlistview.R;
import com.example.wechatlistview.controls.SimperListAdapter;
import com.example.wechatlistview.events.strategys.ClickStrategy;
import com.example.wechatlistview.resmanager.DataSource;
import com.example.wechatlistview.resmanager.itemType;
import com.example.wechatlistview.tools.ArrayTools;
import com.example.wechatlistview.events.controllers.ClickController;

public class fg_chats extends Fragment {


    private Context context;
    private boolean isAdapterDeployed = false;
    private SimpleAdapter simpleAdapter;

    /**
     * 控件
     */
    private ListView listView = null;
    private ImageButton btn_add;
    private View searchView;
    /**
     * 键数组
     * 头像组
     * 名称组
     * 日期组
     * 聊天内容组
     * 控件组
     */
    private String[] keyAry;
    private Object[] headAry;
    private String[] nameAry;
    private String[] dateAry;
    private String[] contentAry;
    private int[] ctrlAry;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_chats, container, false);

        listView = v.findViewById(R.id.chatsList);
        btn_add = v.findViewById(R.id.chats_btn_add);

        keyAry = new String[]{
                "head", "name", "date", "content"
        };
        headAry = new Object[]{
                R.mipmap.heads_chats_txcloud, R.mipmap.heads_chats_subscription, R.mipmap.heads_chats_wechatpay,
                R.mipmap.heads_chats_hut, R.mipmap.heads_chats_healthcard,
                R.mipmap.heads_contacts_1, R.mipmap.heads_contacts_2, R.mipmap.heads_contacts_3,
                R.mipmap.heads_contacts_4, R.mipmap.heads_contacts_5, R.mipmap.heads_contacts_6,
                R.mipmap.heads_contacts_7, R.mipmap.heads_contacts_8, R.mipmap.heads_contacts_9
        };
        ctrlAry = new int[]{R.id.chatsImg, R.id.chatsName, R.id.chatsDate, R.id.chatsContent};

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();

        DataSource dataSource = new DataSource(context);

        //region 聊天列表初始化
        nameAry = ArrayTools.mergeArray(dataSource.GetStrArrayFromRes(R.array.chats_name), dataSource.GetStrArrayFromRes(R.array.contacts_name));
        dateAry = dataSource.GetStrArrayFromRes(R.array.chats_date);
        contentAry = dataSource.GetStrArrayFromRes(R.array.chats_content);

        if (listView != null) {
            /**
             * 构造万能适配器
             */
            searchView = View.inflate(context, R.layout.headview_chats, null);
            listView.addHeaderView(searchView);
            if (!isAdapterDeployed) {
                SimperListAdapter simperListAdapter = new SimperListAdapter(context, R.layout.listview_chats, keyAry, new Object[][]{headAry, nameAry, dateAry, contentAry}, ctrlAry);
                simpleAdapter = simperListAdapter.GetAdapter();
                isAdapterDeployed = true;
            }


            listView.setAdapter(simpleAdapter);

        }
        //endregion


        btn_add.setOnClickListener(new ClickController.BtnBuilder(context, ClickStrategy.BtnStratgy.CHATS_ADD));

    }
}
